<template>
    <div class="menus-box">
        <div class="role-right">
            <!--查询面板-->
            <SearchPanel ref="searchPanel" :selectOptions="selectOptions" :search-config="searchConfig" @startSearch="startSearch">
            </SearchPanel>

            <!-- 查询结果 -->
            <ResultListPanel
                tableRef="table"
                ref="multipleTable"
                :data="dataList ? dataList : []"
                :listConfig="resultColConfig"
                :listLoading="listLoading"
                @actionClick="actionClick"
                @paginationChange="paginationChange"
                :tagTextExchange="tagTextExchange"
            ></ResultListPanel>
        </div>
        <audit v-if="auditVisible" ref="auditDom" @refreshDataList="getList"></audit>
    </div>
</template>
<script>
import { INDEX_SEARCH_CONFIG, INDEX_SEARCH_RESULT_ITEMS, INDEX_SEARCH_SELECT_OPTIONS_CONFIG } from './config';
import ResultListPanel from '../../components/ResultListPanel';
import SearchPanel from '../../components/SearchPanel';
import API from '@/api';
import cloneDeep from 'lodash/cloneDeep';
import { mapGetters } from 'vuex';
import audit from './audit';

import { commercialUserType, userType, statusType } from '../../utils/util';

export default {
    components: {
        audit,
        ResultListPanel,
        SearchPanel
    },
   
    data() {
        return {
            dataListLoading: false,
            dialogVisible: false,
            dialogTitle: '新增',
            auditVisible: false,
            // 搜索项配置
            searchConfig: INDEX_SEARCH_CONFIG,
            resultColConfig: INDEX_SEARCH_RESULT_ITEMS,
            listLoading: false, // 数据列表显示状态
            dataList: [], //  数据列表
            /* 分页查询数据 */
            pageSize: 10,
            pageNum: 1,
            totalPage: 0,
            /* 查询条件参数 */
            dataForm: {
                name: '',
                merchantPhone: '',
                status: null // null 为 status 的默认值，{'value': null, 'label': '全部'}
            },
            type: 1,
            selectOptions: INDEX_SEARCH_SELECT_OPTIONS_CONFIG
        };
    },

    mounted() {
        this.getList();
        this.$set(this.$refs.searchPanel['dataForm'], 'status', null); // null 为 status 的默认值，{'value': null, 'label': '全部'}
    },

    watch: {
        //- 解决 同一页面，数据不刷新问题

        // 监听相同路由下参数变化的时候，从而实现异步刷新
        $route(to, from) {
            this.getList();
            this.nickname = this.$route.query.nickname; //修改页面标题
        }
    },

    methods: {
        /***
         * 触发 行操作按钮
         * @param data
         * @param item
         */
        actionClick([data, item]) {
            if (item.alias === 'audit') {
                this.auditHandle(data);
            }
        },
        tagTextExchange([data, item]) {
            
            //状态
            if (item.alias == 'status') {
                return statusType(data.status);
            }

            //角色
            if (item.alias == 'userType') {
                return userType(data.userType);
            }

            //类型
            if (item.alias == 'merchantType') {
                return commercialUserType(data.merchantType);
            }
        },

        // 审核
        auditHandle(items) {
            console.log(items);
            this.auditVisible = true;
            this.$nextTick(() => {
                // 审核
                this.$refs.auditDom.init(items.userId);
            });
        },

        /**
         * 分页器发生改变
         * @param page
         */
        paginationChange(page) {
            this.pageSize = page.pageSize;
            this.pageNum = page.pageNo;
            this.getList();
        },
        /***
         * 搜索面板出发搜索事件
         * @param params
         */
        startSearch(params) {
            this.dataForm = params;
            this.pageNum = 1;
            this.$set(this.$refs.multipleTable['pageParams'], 'pageNum', 1);

            this.getList();
        },

        getList() {
            let params = cloneDeep(this.dataForm);
             
            let queryParams={};
            let postParams = {};
            postParams.pageNum = this.pageNum;
            postParams.pageSize = this.pageSize;

            switch (
                this.$route.path // 商户类型. BASE: 基地商户; ACAPITAL: 农资商户; GENERAL: 普通用户; PRDBUY: 农产品采购方 ；FUND 资金方
            ) {
                case '/commercialInfoAudit/index':
                    //基地商户信息审核
                    queryParams.merchantType = 'BASE';
                    queryParams.phone=params.merchantPhone;
                    queryParams.userId=params.name;
                    queryParams.status=params.status;

                    postParams.query = queryParams;
                    break;

                case '/nzshManger/merchanInfoAudit/index':
                    //农资商户管理 - 农资商户信息审核
                    queryParams.merchantType = 'ACAPITAL';
                    queryParams.phone=params.merchantPhone;
                    queryParams.userId=params.name;
                    queryParams.status=params.status;

                    postParams.query = queryParams;

                    break;

                case '/nzshManger/purchaserInfoAudit/index':
                    //农产品采购方管理 - 农产品采购方信息审核
                    queryParams.merchantType = 'PRDBUY';
                    queryParams.phone=params.merchantPhone;
                    queryParams.userId=params.name;
                    queryParams.status=params.status;

                    postParams.query = queryParams;

                    break;

                case '/zjfShManger/zjfShInfoAudit/index':
                    //资金方商户管理 - 资金方商户信息审核
                    queryParams.merchantType = 'FUND';
                    queryParams.phone=params.merchantPhone;
                    queryParams.userId=params.name;
                    queryParams.status=params.status;

                    postParams.query = queryParams;

                    break;
            }

            this.listLoading = true;
            API.nzshManger.getAuditList(postParams).then((res) => {
                console.log(res);
                this.listLoading = false;
                this.loading = false;
                if (res.code == '0000') {
                    this.totalPage = res.result.total;
                    this.dataList = res.result.rows;
                    console.log(this.dataList);
                    this.$set(this.$refs.multipleTable['pageParams'], 'total', res.result.total);
                }
            });
        }
    }
};
</script>
<style scoped>
.menus-box {
    background: #fff;
    padding: 20px;
    min-height: 100%;
}
</style>